 
#pragma once

#include "Singleton.h"
#include "tool/RingQueue.h"
#include "tool/Logger.h"
#include <iostream>
#include <mutex>
#include <functional>
#include <thread>

typedef std::function<void ()> SyncFunctor;
typedef RingQueue<SyncFunctor, 1000> SyncFuncQueue;
 
class LogMgr: public Singleton<LogMgr>
{
    protected:
        friend class Singleton<LogMgr>;
        LogMgr() {}
        ~LogMgr() {}
    public:
        int Init(const std::string& conf_file);
        Logger& GetLogger() { return *_logger; }
	    void Run();
        void _Run();
        SyncFuncQueue m_logs;        // 也先给个1000再说吧
    private:
        std::thread m_thread;
        bool m_exit = false;	// 是否结束
        Logger* _logger;
};

// 日志线程打印日志
#define LOG_DEF(level, log, s) \
 { \
    std::ostringstream logs; \
    logs << __FILE__ << ":" << __LINE__ << "|" << s; \
    char* p = new char[logs.str().size() + 1]; \
    bzero(p, logs.str().size() + 1); \
    strcpy(p, logs.str().c_str());  \
    LogMgr::instance().m_logs.Push([p]{ LogMgr::instance().GetLogger().write(level,p); delete []p; }); \
}

#define LOG_INFO(s)     LOG_DEF(LogInfo,    GAME_LOG_NAME,  s)
#define LOG_WARN(s)     LOG_DEF(LogWarn,    GAME_LOG_NAME,  s)
#define LOG_ERROR(s)    LOG_DEF(LogError,   GAME_LOG_NAME,  s)
#define LOG_FATAL(s)    LOG_DEF(LogFatal,   GAME_LOG_NAME,  s)
